package com.shujia.wyh.selenium_demos;

/*
    使用自动化爬取京东商品信息，存储到数据库表中

    首页网址：https://www.jd.com/

    任务：
    1、将上课的案例复现，爬取前5页数据
    2、编写sql，至少分析出3指标
    3、使用fineBI绘制柱状图，饼状图，将绘制的图

 */

import com.shujia.wyh.utils.MySqlTool;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.List;
import java.util.Scanner;

class JDThread extends Thread {
    @Override
    public void run() {
        Scanner sc = new Scanner(System.in);
        // 在编写代码之前，确保正版的谷歌浏览器已经安装，以及驱动包下载好
        //1. 指定驱动包的位置
        System.setProperty("webdriver.chrome.driver", "D:\\Code\\IDEAProjects\\bigdata25-project\\bigdata25-project\\mysql\\src\\main\\java\\com\\shujia\\wyh\\utils\\chromedriver.exe");

        //后台打开浏览器
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless"); // 启用无头模式

        WebDriver driver = new ChromeDriver(options);

//        //2. 通过驱动打开浏览器
//        WebDriver driver = new ChromeDriver();
//
//        Dimension dimension = new Dimension(1920, 1080);
//        driver.manage().window().setSize(dimension);

        //3. 指定网址url并打开
        //driver中就保存了我们看到了页面中所有的元素，其实就是刚刚看到的html代码
        driver.get("https://www.jd.com/");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.print("请输入要搜索的商品：");
        String info = sc.next();

        //通过xpath定位到搜索框
        driver.findElement(By.xpath("//input[@id='key']")).sendKeys(info);

        //定位到搜索按钮
        //clstag='h|keycount|h|keycount|head|search_a'
        driver.findElement(By.xpath("//button[@clstag='h|keycount|h|keycount|head|search_a']")).click();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //能点击的下一页标签：<a class="pn-next" onclick="SEARCH.page(3, true)" href="javascript:;" title="使用方向键右键也可翻到下一页哦！"><em>下一页</em><i>&gt;</i></a>
        //不能点击的下一页的标签：<a class="pn-next disabled"><em>下一页</em><i>&gt;</i></a>

        int i = 1;
        // TODO: 如果要一直爬到最后，这里的条件改成true
        while (i <= 3) {
            try {
                getPageData(driver);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //定位到下一页标签
            WebElement element = driver.findElement(By.xpath("//div[@id='J_bottomPage']/span/a[last()]"));
            String aClass = element.getAttribute("class");
            if ("pn-next".equals(aClass)) {
                element.click();
            } else {
                break;
            }
            System.out.println("-------------------------第 " + i + "页获取完毕-------------------------");

            i++;
        }


        //关闭浏览器
        driver.quit();
    }

    public void getPageData(WebDriver driver) throws Exception {
        //拉到底部
        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
        jsExecutor.executeScript("window.scrollTo(0, document.body.scrollHeight)");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        //获取预编译操作数据库对象
        Statement statement = MySqlTool.getStatement();

        //获取所有的li信息
        List<WebElement> elements = driver.findElements(By.xpath("//ul[@class='gl-warp clearfix']/li"));

        //遍历集合得到每一个商品
        for (WebElement element : elements) {
            //获取标签中的文本信息
//            String text = element.getText();
//            System.out.println(text);

            //获取每个li标签下的所有div
            List<WebElement> elements1 = element.findElements(By.xpath("./div/div"));

            String name = null;
            String price = null;
            String shop = null;
            String commit = null;

            //遍历每个div
            for (WebElement webElement : elements1) {
                //判断div的class属性值是否是p-name p-name-type-2
                //getAttribute()获取元素标签的属性值
                String s = webElement.getAttribute("class");

                //获取商品的名称
                if ("p-name p-name-type-2".equals(s)) {
                    name = webElement.getText();
                    System.out.println(name);
                }else if ("p-price".equals(s)) {
                    //获取商品的价格p-price
                    price = webElement.getText();
                    price = price.substring(1);

                    if(price.contains("￥")){
                        price = price.substring(0,price.indexOf("￥"));
                    }

                    System.out.println(price);
                }else if ("p-shop".equals(s)) {
                    //获取商品的店铺名称p-shop
                    shop = webElement.getText();
                    System.out.println(shop);
                }else if ("p-commit".equals(s)) {
                    //获取商品的评论数p-commit
                    commit = webElement.getText();
                    if(commit.contains("万")){
                        commit = commit.substring(0,commit.indexOf("万"))+"0000"; //50000
                    }else if(commit.contains("+") && !commit.contains("万")){
                        commit = commit.substring(0,commit.indexOf("+"));
                    }else if(!commit.contains("+") && !commit.contains("万")){
                        commit = commit.substring(0,commit.indexOf("条"));
                    }

                    if(commit.contains("手")){
                        commit = commit.replace("去看二手","");
                    }

                    System.out.println(commit);
                }
            }

            String sql = "insert into jd_goods(goods_name,goods_price,goods_shop,goods_comments) values('"+name+"','"+price+"','"+shop+"','"+commit+"')";

            //执行sql语句
            statement.executeUpdate(sql);
            System.out.println("===================================================================================");
        }
    }
}

public class JDSeleniumDemo {
    public static void main(String[] args) {
        new JDThread().start();
    }
}
